﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using WlanDesign;

namespace WlanDesignDll
{
    class DrawingExportCommandHandler: System.Windows.Input.ICommand
    {
        #region ICommand 成员

        public bool CanExecute(object parameter)
        {
            return true;
            //throw new NotImplementedException();
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            /*
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            //选择矩形区域，获取矩形的两个对角点
            PromptPointResult pPResult = ed.GetPoint("选择起点");
            Point3d ptStart = pPResult.Value;

            PromptPointResult pPResult2 = ed.GetCorner("\n选择对角点", ptStart);
            Point3d ptEnd = pPResult2.Value;

            string[] name1 = doc.Name.Split('.');
            string iniName1 = name1[0] + ".ini";
            // 写入ini文件
            WriteIniTxt(iniName1, "drawingexport", "building_start", ptStart.ToString());
            WriteIniTxt(iniName1, "drawingexport", "building_end", ptEnd.ToString());
            */

            //if (ptStart != null && ptEnd != null)
            //{    
                    AcadApplication acAppComObj = null;
                    const string strProgId = "AutoCAD.Application.18";
                    // Get a running instance of AutoCAD
                    try
                    {
                        acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
                    }
                    catch // An error occurs if no instance is running
                    {
                        try
                        {
                            // Create a new instance of AutoCAD
                            acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);

                        }
                        catch
                        {
                            // If an instance of AutoCAD is not created then message and exit
                            System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created.");
                            return;
                        }
                    }
                    AcadDocument acDocComObj;

                    acDocComObj = acAppComObj.ActiveDocument;
                    acDocComObj.SendCommand("DrawingExport ");
                    //WriteIniTxt(iniName1, "drawingexport", "building_start", "");
                    //WriteIniTxt(iniName1, "drawingexport", "building_end", "");
            //}
            //throw new NotImplementedException();
        }
        //
        // Summery
        // 加载信息到ini文件
        //
        public void WriteIniTxt(string sPath, string Section, string Key, string value)
        {
            string ServerName = value;

            IniFileRW ini = new IniFileRW(sPath);

            ini.IniWriteValue(Section, Key, ServerName);
        }
        /// <summary>
        /// 选择包含在矩形区域内的实体
        /// </summary>
        /// <param name="pt1">矩形的左上角</param>
        /// <param name="pt2">矩形的右下角</param>
        /// <param name="doc">当前文档</param>
        /// <returns></returns>
        public static DBObjectCollection InWindow(Point3d pt1, Point3d pt2, Document doc)
        {
            Database db = doc.Database;
            Editor ed = doc.Editor;
            Entity entity = null;
            DBObjectCollection entityCollection = new DBObjectCollection();
            PromptSelectionResult ents = ed.SelectWindow(pt1, pt2);
            if (ents.Status == PromptStatus.OK)
            {
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    SelectionSet ss = ents.Value;
                    foreach (ObjectId id in ss.GetObjectIds())
                    {
                        entity = trans.GetObject(id, OpenMode.ForWrite, true, true) as Entity;
                        if (entity != null)
                        { entityCollection.Add(entity); }
                    }
                    trans.Commit();
                }
            }
            return entityCollection;
        }
        #endregion
    }
}
